<?php
/**
==============================================================================
 * Class FormComponent GracePHP的输入模型组件
 * @author 肖永标
 * @date 2014-12-8
==============================================================================
 */
class InputComponent extends Component {

    /** 获取过滤后的get
     * @param $field
     * @return array|string
     */
    public function get($field)
    {
        if(!is_array(@$_GET[$field]))
            return $this->_clean($_GET[$field]);
        else
            return array_map(array($this, '_clean'), @$_GET[$field]);
    }


    /** 获取过滤后的post
     * @param $field
     * @return array|string
     */
    public function post($field)
    {
        if(!is_array(@$_POST[$field]))
            return $this->_clean($_POST[$field]);
        else
            return array_map(array($this, '_clean'), @$_POST[$field]);
    }


    /** 获取cookie
     * @param $field
     * @return array|string
     */
    public function cookie($field)
    {
        if(!is_array(@$_COOKIE[$field]))
            return $this->_clean($_COOKIE[$field]);
        else
            return array_map(array($this, '_clean'), @$_COOKIE[$field]);
    }
  
    
    /** session
     * @param $field
     * @return array|string
     */
     public function session($field)
     {
        if(!is_array(@$_SESSION[$field]))
            return $this->_clean($_SESSION[$field]);
        else
            return array_map(array($this, '_clean'), @$_SESSION[$field]);
     }
    
    
    /** 获取当前ip地址
     *  @return string
     */
    public function ipAddress()
    {
        return $_SERVER['SERVER_ADDR'];    
    }
    
    /** 获取server变量
     * @return array|string
     */
     public function server($field)
     {
        if(!is_array(@$_SERVER[$field]))
            return $this->_clean($_SESSION[$field]);
        else
            return array_map(array($this, '_clean'), @$_SERVER[$field]);
     }
    
    
    /** 过滤非法数据
     * @param mixed $val 需要过滤的值
     * @return string
     */
    private function _clean($val)
    {
        if(!get_magic_quotes_gpc())
            return htmlspecialchars(addslashes($val));
        else
            return htmlspecialchars($val);
    }

    public function allowLoad()
    {
        return true;
    }
}